package com.myorg.mapred;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {
	public static class TokenizerMapper extends Mapper<Object, Text, Text, LongWritable> {

		private final static LongWritable one = new LongWritable(1L);
		private Text word = new Text();

		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			StringTokenizer itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {
				word.set(itr.nextToken());
				context.write(word, one);
			}
		}
	}

	public static class LongSumReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
		private LongWritable result = new LongWritable();

		public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,
				InterruptedException {
			long sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		if (otherArgs.length < 2) {
			System.err.println("Usage: wordcount <in> [<in>...] <out>");
			System.exit(2);
		}
		Job job = Job.getInstance(conf, "word count");
		job.setJarByClass(WordCount.class);
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(LongSumReducer.class);
		job.setReducerClass(LongSumReducer.class);

		job.setInputFormatClass(CustomFileInputFormat.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);
		job.setOutputFormatClass(SequenceFileOutputFormat.class);
		for (int i = 0; i < otherArgs.length - 1; ++i) {
			CustomFileInputFormat.addInputPath(job, new Path(otherArgs[i]));
		}
		SequenceFileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));

		while (!job.waitForCompletion(true)) {
			Thread.sleep(1000);
		}

		FileSystem fs = FileSystem.get(new Configuration());
		FileStatus[] fss = fs.listStatus(new Path(otherArgs[otherArgs.length - 1]));
		for (FileStatus status : fss) {
			Path path = status.getPath();
			if (!path.getName().endsWith("_SUCCESS")) {
				SequenceFile.Reader reader = new SequenceFile.Reader(conf, SequenceFile.Reader.file(path));
				Text key = new Text();
				LongWritable value = new LongWritable();
				while (reader.next(key, value)) {
					System.out.println(key.toString() + " --> " + value.get());
				}
				reader.close();
				fs.deleteOnExit(path);
			}
		}
	}
}